package com.shiroa.controller;

import com.shiroa.common.R;
import com.shiroa.entity.SysRole;
import com.shiroa.service.ISysRoleService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 * 角色信息表 前端控制器
 * </p>
 *
 * @author baomidou
 * @since 2023-05-15
 */
@RestController
@RequestMapping("/sysRole")
public class SysRoleController {
    @Autowired
    private ISysRoleService roleService;

    //    @RequiresPermissions("role:add")
    @PreAuthorize("hasAuthority('role:add')")
    @PostMapping
    public R add(@RequestBody SysRole role) {
        try {
            roleService.save(role);
        } catch (Exception e) {
            return R.err(e.getMessage());
        }
        return R.ok();
    }

    //    @RequiresRoles("manager")
    @PreAuthorize("hasAuthority('role:delete')")
    @DeleteMapping("/{roleId}")
    public R del(@PathVariable Long roleId) {
        roleService.removeById(roleId);
        return R.ok();
    }

    //    @RequiresPermissions("role:update")
    @PreAuthorize("hasAuthority('role:update')")
    @PutMapping
    public R update(@RequestBody SysRole role) {
        roleService.saveOrUpdate(role);
        return R.ok();
    }

    //    @RequiresPermissions("role:list")
    @PreAuthorize("hasAuthority('role:list')")
    @GetMapping
    public R getAll() {
        List<SysRole> list = roleService.list();
        return R.ok(list);
    }

}